package com.visizen.wisdomclass.server.client;

import com.visizen.wisdomclass.server.model.WcTopicHot;
import com.visizen.wisdomclass.server.model.WcTopicHotExample;

import java.util.List;

import com.visizen.wisdomclass.server.model.dto.TopicExt;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface WcTopicHotMapper {
    @Select("select * from wc_topic_hot where topic_id=#{topicId} limit 1")
    WcTopicHot selectByTopicId(@Param("topicId") Long topicId);

    @Update({
            "update wc_topic_hot" +
                    " set create_time = #{create_time,jdbcType=TIMESTAMP}," +
                    "expiration_time = #{expiration_time,jdbcType=TIMESTAMP}" +
                    "where topic_id = #{topic_id,jdbcType=BIGINT}"
    })
    int updateByTopicId(WcTopicHot record);

    /**
     * 获取热门话题的信息
     *
     * @param start
     * @param end
     * @return
     */
    @Select(" SELECT\n" +
            "  t.*\n" +
            " FROM wc_topic AS t\n" +
            " WHERE t.topic_id IN (SELECT\n" +
            "                       topic_id\n" +
            "                     FROM wc_topic_hot\n" +
            "                     WHERE expiration_time > current_date order by create_time desc) LIMIT #{start},#{end}")
    @Results({
            @Result(property = "topic_id", column = "topic_id"),
            @Result(property = "discussCount", column = "topic_id", one = @One(select = "selectDiscussCount")),
            @Result(property = "userCount", column = "topic_id", one = @One(select = "selectUserCount"))
    })
    List<TopicExt> hotTopicList(@Param("start") int start,
                                @Param("end") int end);

    /**
     * 查询话题的讨论数
     *
     * @param topicId
     * @return
     */
    @Select("SELECT COUNT(*) FROM wc_discuss\n WHERE topic_id = #{topicId}")
    int selectDiscussCount(@Param("topicId") Long topicId);

    /**
     * 查询话题的参与人数
     *
     * @param topicId
     * @return
     */
    @Select(" SELECT\n" +
            "  count(*)\n" +
            " FROM (\n" +
            "  SELECT\n" +
            "    *\n" +
            "  FROM wc_discuss_log_see\n" +
            "  WHERE discuss_id IN (SELECT\n" +
            "                         discuss_id\n" +
            "                       FROM wc_discuss\n" +
            "                       WHERE topic_id = #{topicId})\n" +
            "  GROUP BY user_id) as a;")
    int selectUserCount(@Param("topicId") Long topicId);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_hot
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcTopicHotSqlProvider.class, method = "countByExample")
    int countByExample(WcTopicHotExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_hot
     *
     * @mbggenerated
     */
    @DeleteProvider(type = WcTopicHotSqlProvider.class, method = "deleteByExample")
    int deleteByExample(WcTopicHotExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_hot
     *
     * @mbggenerated
     */
    @Delete({
            "delete from wc_topic_hot",
            "where hot_id = #{hot_id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long hot_id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_hot
     *
     * @mbggenerated
     */
    @Insert({
            "insert into wc_topic_hot (hot_id, topic_id, ",
            "create_time, expiration_time)",
            "values (#{hot_id,jdbcType=BIGINT}, #{topic_id,jdbcType=BIGINT}, ",
            "#{create_time,jdbcType=TIMESTAMP}, #{expiration_time,jdbcType=TIMESTAMP})"
    })
    int insert(WcTopicHot record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_hot
     *
     * @mbggenerated
     */
    @InsertProvider(type = WcTopicHotSqlProvider.class, method = "insertSelective")
    int insertSelective(WcTopicHot record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_hot
     *
     * @mbggenerated
     */
    @SelectProvider(type = WcTopicHotSqlProvider.class, method = "selectByExample")
    @Results({
            @Result(column = "hot_id", property = "hot_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "topic_id", property = "topic_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "create_time", property = "create_time", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "expiration_time", property = "expiration_time", jdbcType = JdbcType.TIMESTAMP)
    })
    List<WcTopicHot> selectByExample(WcTopicHotExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_hot
     *
     * @mbggenerated
     */
    @Select({
            "select",
            "hot_id, topic_id, create_time, expiration_time",
            "from wc_topic_hot",
            "where hot_id = #{hot_id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column = "hot_id", property = "hot_id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "topic_id", property = "topic_id", jdbcType = JdbcType.BIGINT),
            @Result(column = "create_time", property = "create_time", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "expiration_time", property = "expiration_time", jdbcType = JdbcType.TIMESTAMP)
    })
    WcTopicHot selectByPrimaryKey(Long hot_id);


    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_hot
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcTopicHotSqlProvider.class, method = "updateByExampleSelective")
    int updateByExampleSelective(@Param("record") WcTopicHot record, @Param("example") WcTopicHotExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_hot
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcTopicHotSqlProvider.class, method = "updateByExample")
    int updateByExample(@Param("record") WcTopicHot record, @Param("example") WcTopicHotExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_hot
     *
     * @mbggenerated
     */
    @UpdateProvider(type = WcTopicHotSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(WcTopicHot record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table wc_topic_hot
     *
     * @mbggenerated
     */
    @Update({
            "update wc_topic_hot",
            "set topic_id = #{topic_id,jdbcType=BIGINT},",
            "create_time = #{create_time,jdbcType=TIMESTAMP},",
            "expiration_time = #{expiration_time,jdbcType=TIMESTAMP}",
            "where hot_id = #{hot_id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(WcTopicHot record);
}